$(function () {
    $("#jqGrid").jqGrid({
        url: '../bespeak/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'bespeakId', index: 'bespeakId', width: 50, key: true ,hidden:true},
			{ label: '用户', name: 'username', index: 'username', width: 80 },
			{ label: '座位编号', name: 'seatCode', index: 'seatCode', width: 80 },
			{ label: '教室', name: 'name', index: 'name', width: 80 },
			{ label: '预约时间', name: 'subscribeTime', index: 'subscribeTime', width: 80 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		bespeak: {
			userId:"",
      libraryId:"",
      seatId:"",
      subscribeTime:""
		},
    seatId:""
	},
	computed: {
    options: function () {
      var array = new Array();
      $.ajax({
				url:"../bespeak/getUserList",
        async: false,
        success: function(data){
          $.each(data.userEntities, function (index, ele) {
            var temp = {};
            temp['text'] = ele.username;
            temp['value'] = ele.userId;
            array.push(temp);
          });
        }
      });
      return array;
    },
    libraryOptions: function () {
      var array = new Array();
      $.ajax({
				url:"../bespeak/getLibraryList",
        async: false,
        success: function(data){
          $.each(data.libraryEntities, function (index, ele) {
            var temp = {};
            temp['text'] = ele.name;
              temp['value'] = ele.id;
            array.push(temp);
          });
        }
      });
      return array;
    }
  },
  methods: {
    prochange:function(libraryId){
    	if(libraryId){
        var array = new Array();
        $.ajax({
          url: "../bespeak/getSeatListByLibraryId",
          async: false,
          data:{id:libraryId},
          success: function (data) {
            var array = new Array();
            $.each(data.seatEntities, function (index, ele) {
              var temp = {};
              temp['text'] = ele.code;
              temp['value'] = ele.id;
              array.push(temp);
            });
            vm.seatId = array;
          }
        })
			}
		},
		query: function () {
			vm.reload();
		},
		add: function(){
        vm.showList = false;
        vm.title = "新增";
        vm.bespeak = {
          userId:"",
          libraryId:"",
          seatId:"",
          subscribeTime:""
        };
        vm.seatId="";
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
    },
		saveOrUpdate: function (event) {
			var url = vm.bespeak.id == null ? "../bespeak/save" : "../bespeak/update";
      if(vm.bespeak.userId===undefined||vm.bespeak.userId===''||vm.bespeak.userId===null){
        alert("请选择预约用户");
        return;
      }
      if(vm.bespeak.libraryId===undefined||vm.bespeak.libraryId===''||vm.bespeak.libraryId===null){
        alert("请选择教室");
        return;
      }
      if(vm.bespeak.seatId===undefined||vm.bespeak.seatId===''||vm.bespeak.seatId===null){
        alert("请选择座位");
        return;
      }
      if(vm.bespeak.subscribeTime===undefined||vm.bespeak.subscribeTime===''||vm.bespeak.subscribeTime===null){
        alert("请选择预约时间");
        return;
      }

			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.bespeak),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../bespeak/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../bespeak/info/"+id, function(r){
                vm.bespeak = r.bespeak;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});